package com.sky.service.impl;

import com.sky.mapper.CommonMapper;
import com.sky.service.CommonService;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName CommonServiceImpl
 * @Description TODO
 * @Author 23207
 * @Date 2024/12/11 9:06
 * @Version 1.0
 */
@Slf4j
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @Override
    public String upload(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename(); // 原始文件名
            String type = fileName.substring(fileName.lastIndexOf('.'), fileName.length()); // 文件类型
            String newName = UUID.randomUUID().toString() + type; // 新文件名
            return aliOssUtil.upload(file.getBytes(), newName);
        } catch (IOException e) {
            // e.printStackTrace();
            log.error("图片上传失败..{}", e);
        }
        return null;
    }
}
